package de.fhin.mas.agents.gripper.behaviours;

import jade.core.Agent;
import jade.core.behaviours.CyclicBehaviour;
import jade.lang.acl.ACLMessage;
import de.fhin.mas.MASEvents;
import de.fhin.mas.agents.gripper.GripperAgent;

public class ListenToMessages extends CyclicBehaviour {

	private static final long serialVersionUID = 6866513985599391230L;

	private final GripperAgent gripperAgent;
	private final MoveGrapplerToDestination toDestination;
	private final MoveGrapplerToStandbyPosition toStandby;
	private final MoveGrapplerToTransportPosition toTransport;

	public ListenToMessages(Agent superAgent) {
		super(superAgent);
		gripperAgent = (GripperAgent) superAgent;
		toStandby = new MoveGrapplerToStandbyPosition(gripperAgent);
		toTransport = new MoveGrapplerToTransportPosition(gripperAgent);
		toDestination = new MoveGrapplerToDestination(gripperAgent, toStandby, toTransport);
	}

	@Override
	public void action() {
		String grapplerStatus = gripperAgent.getGrapplerStatus();
		String resumeStatus = gripperAgent.getResumeStatus();
		ACLMessage msg = myAgent.receive();
		if (msg != null && msg.getConversationId() != null) {
			String conversationID = msg.getConversationId();
			// SYSTEM_STOP_REQUIRED
			if (conversationID.equals(MASEvents.SYSTEM_STOP_REQUIRED.getName()) && msg.getPerformative() == ACLMessage.REQUEST) {
				ACLMessage agreeMsg = msg.createReply();
				agreeMsg.setPerformative(ACLMessage.AGREE);
				gripperAgent.send(agreeMsg);
				gripperAgent.printLn("AGREE SYSTEM_STOP_REQUIRED");
				// Gripper befindet sich in Standby-/Transportposition
				if (grapplerStatus.equals("STANDBY") || grapplerStatus.equals("TRANSPORT") || grapplerStatus.equals("PUTDOWN") || grapplerStatus.equals("TAKEUP")) {
					if (grapplerStatus.equals("STANDBY")) {
						gripperAgent.setResumeStatus("SSR_STANDBY");
					} else if (grapplerStatus.equals("TRANSPORT")) {
						gripperAgent.setResumeStatus("SSR_TRANSPORT");
					} else if (grapplerStatus.equals("PUTDOWN")) {
						gripperAgent.setResumeStatus("SSR_PUTDOWN");
					} else {
						gripperAgent.setResumeStatus("SSR_TAKEUP");
					}
					gripperAgent.setGrapplerStatus("STOPPED");
					ACLMessage informDone = msg.createReply();
					informDone.setPerformative(ACLMessage.INFORM);
					informDone.setContent("INFORM-DONE");
					gripperAgent.send(informDone);
					gripperAgent.printLn("INFORM-DONE SYSTEM_STOP_REQUIRED");
				} else {
					ACLMessage informFailure = msg.createReply();
					informFailure.setPerformative(ACLMessage.FAILURE);
					gripperAgent.send(informFailure);
					gripperAgent.printLn("FAILURE SYSTEM_STOP_REQUIRED");
				}
			}
			// RESUME
			else if (conversationID.equals(MASEvents.RESUME.getName())) {
				if (msg.getPerformative() == ACLMessage.CFP) {
					if (grapplerStatus.equals("STOPPED")) {
						ACLMessage propose = msg.createReply();
						propose.setPerformative(ACLMessage.PROPOSE);
						gripperAgent.send(propose);
						gripperAgent.printLn("PROPOSE RESUME");
					} else {
						ACLMessage refuse = msg.createReply();
						refuse.setPerformative(ACLMessage.REFUSE);
						gripperAgent.send(refuse);
						gripperAgent.printLn("REFUSE RESUME");
					}
				} else if (msg.getPerformative() == ACLMessage.ACCEPT_PROPOSAL) {
					if (resumeStatus.equals("SSR_STANDBY") || resumeStatus.equals("SSR_TRANSPORT") || resumeStatus.equals("SSR_PUTDOWN") || resumeStatus.equals("SSR_TAKEUP")) {
						ACLMessage informDone = msg.createReply();
						informDone.setPerformative(ACLMessage.INFORM);
						informDone.setContent("INFORM-DONE");
						gripperAgent.send(informDone);
						gripperAgent.printLn("INFORM-DONE RESUME");
						if (resumeStatus.equals("SSR_STANDBY")) {
							gripperAgent.setGrapplerStatus("STANDBY");
						} else if (resumeStatus.equals("SSR_TRANSPORT")) {
							gripperAgent.setGrapplerStatus("TRANSPORT");
						} else if (resumeStatus.equals("SSR_PUTDOWN")) {
							toStandby.moveGrappler();
						} else {
							toTransport.moveGrappler();
						}
						gripperAgent.setResumeStatus("NULL");
					} else {
						ACLMessage failure = msg.createReply();
						failure.setPerformative(ACLMessage.FAILURE);
						gripperAgent.send(failure);
						gripperAgent.printLn("FAILURE RESUME");
					}
				}
			}
			// NEW_ORDER
			else if (conversationID.equals(MASEvents.NEW_ORDER.getName())) {
				if (msg.getPerformative() == ACLMessage.CFP) {
					gripperAgent.setResumeStatus("NULL");
					if (grapplerStatus.equals("ABORTED")) {
						gripperAgent.setGrapplerStatus("STANDBY");
					}
					if (gripperAgent.isSizeable(msg) == true) {
						ACLMessage propose = msg.createReply();
						propose.setPerformative(ACLMessage.PROPOSE);
						propose.setConversationId(MASEvents.NEW_ORDER.getName());
						gripperAgent.send(propose);
						gripperAgent.printLn("PROPOSE NEW_ORDER");
					} else {
						ACLMessage refuse = msg.createReply();
						refuse.setPerformative(ACLMessage.REFUSE);
						refuse.setConversationId(MASEvents.NEW_ORDER.getName());
						gripperAgent.send(refuse);
						gripperAgent.printLn("REFUSE NEW_ORDER");
					}
				} else if (msg.getPerformative() == ACLMessage.ACCEPT_PROPOSAL) {
					ACLMessage informDone = msg.createReply();
					informDone.setPerformative(ACLMessage.INFORM);
					informDone.setContent("INFORM-DONE");
					gripperAgent.send(informDone);
					gripperAgent.printLn("INFORM_DONE NEW_ORDER");
				}

			}
			// TARGET_OBJECT_REACHED
			else if (conversationID.equals(MASEvents.TARGET_OBJECT_REACHED.getName()) && msg.getPerformative() == ACLMessage.INFORM) {
				if (grapplerStatus.equals("STANDBY") && !grapplerStatus.equals("STOPPED") && !grapplerStatus.equals("ABORTED")) {
					toDestination.moveGrappler("TAKEUP");
				} else if (grapplerStatus.equals("TRANSPORT")) {
					gripperAgent.printLn("Object already loaded !");
				}
			}
			// STORAGE_REACHED
			else if (conversationID.equals(MASEvents.STORAGE_REACHED.getName()) && msg.getPerformative() == ACLMessage.INFORM) {
				if (grapplerStatus.equals("TRANSPORT") && !grapplerStatus.equals("STOPPED") && !grapplerStatus.equals("ABORTED")) {
					toDestination.moveGrappler("PUTDOWN");
				} else if (grapplerStatus.equals("STANDBY")) {
					gripperAgent.printLn("No object to unload !");
				}
			}
			// ABORT_AND_GO_TO
			else if (conversationID.equals(MASEvents.ABORT_AND_GO_TO.getName()) && msg.getPerformative() == ACLMessage.REQUEST) {
				ACLMessage agree = msg.createReply();
				agree.setConversationId(MASEvents.ABORT_AND_GO_TO.getName());
				agree.setPerformative(ACLMessage.AGREE);
				gripperAgent.send(agree);
				gripperAgent.printLn("AGREE ABORT_AND_GO_TO");
				gripperAgent.printLn(gripperAgent.getGrapplerStatus());
				gripperAgent.printLn(gripperAgent.getResumeStatus());
				// Gripper befindet sich in Standby-/Transportposition
				if (grapplerStatus.equals("STANDBY") || grapplerStatus.equals("TRANSPORT") || grapplerStatus.equals("PUTDOWN") || grapplerStatus.equals("TAKEUP")) {
					if (grapplerStatus.equals("PUTDOWN")) {
						toTransport.action();
						gripperAgent.setResumeStatus("AGT_TRANSPORT");
					} else if (grapplerStatus.equals("TAKEUP")) {
						toStandby.action();
						gripperAgent.setResumeStatus("AGT_STANDBY");
					}
					gripperAgent.setGrapplerStatus("ABORTED");
					ACLMessage informDone = msg.createReply();
					informDone.setPerformative(ACLMessage.INFORM);
					informDone.setContent("INFORM-DONE");
					gripperAgent.send(informDone);
					gripperAgent.printLn("INFORM-DONE ABORT_AND_GO_TO");
					// Gripper befindet sich in einem Aufnahme-/Ablegevorgang
				} else {
					ACLMessage informFailure = msg.createReply();
					informFailure.setPerformative(ACLMessage.FAILURE);
					gripperAgent.send(informFailure);
					gripperAgent.printLn("INFORM-FAILURE ABORT_AND_GO_TO");
				}
			}
			// MOVEMENT_OF_PLATFORM_NECESSARY
			else if (conversationID.equals(MASEvents.MOVEMENT_OF_PLATFORM_NECESSARY.getName())) {
				if (msg.getPerformative() == ACLMessage.CFP) {
					if (grapplerStatus.equals("STANDBY") || grapplerStatus.equals("TRANSPORT") || grapplerStatus.equals("ABORTED")) {
						// || grapplerStatus.equals("STOPPED") // Es ist nicht sicher, das der Greifer in Transport-/Standbyposition ist
						ACLMessage propose = msg.createReply();
						propose.setPerformative(ACLMessage.PROPOSE);
						gripperAgent.send(propose);
						gripperAgent.printLn("PROPOSE MOVEMENT_OF_PLATFORM_NECESSARY");
					} else {
						ACLMessage refuse = msg.createReply();
						refuse.setPerformative(ACLMessage.REFUSE);
						gripperAgent.send(refuse);
						gripperAgent.printLn("REFUSE MOVEMENT_OF_PLATFORM_NECESSARY");
					}
				} else if (msg.getPerformative() == ACLMessage.ACCEPT_PROPOSAL) {
					ACLMessage informDone = msg.createReply();
					informDone.setPerformative(ACLMessage.INFORM);
					informDone.setContent("INFORM-DONE");
					gripperAgent.send(informDone);
					gripperAgent.printLn("INFORM-DONE MOVEMENT_OF_PLATFORM_NECESSARY");
				}
			}
		}
	}
}
